package com.ptszyxx.popose.module.chat.page.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMManager;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class ChatSetVM extends BaseViewModel<CommonRepository> {
    public String txUserId, userName, userPic;
    public UserResult userEntity;

    public ChatSetVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onClearChatEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onFollowEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onBlackEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onUserDetailCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpUserDetail(this, txUserId));
    public BindingCommand onClearChatCommand = new BindingCommand(()-> uc.onClearChatEvent.call());
    public BindingCommand onReportCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpReport(this, userEntity.getId()+""));
    public BindingCommand onBlackCommand = new BindingCommand(()-> uc.onBlackEvent.call());
    public BindingCommand onFollowCommand = new BindingCommand(()-> requestFollow());
    public BindingCommand onTopCommand = new BindingCommand((o)-> YToastUtil.showShort(""+o));

    //=========================================接口：start===========================================
    /**
     * 请求用户信息
     */
    public void requestUser(){
        Map<String, String> map = new HashMap<>();
        map.put("userid", txUserId);

        HttpUtils.getInstance()
                .data(model.userInfo(map), this,
                        new OnSuccessResult<UserResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserResult> response) {
                                userEntity = response.getData();
                                uc.onFollowEvent.call();
                            }
                        });
    }

    /**
     * 黑名单
     */
    public void requestBlack(){
        Map<String, String> map = new HashMap<>();
        map.put("lhuserid", txUserId);
        HttpUtils.getInstance()
                .data(model.blackUser(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(response.getMsg());
                            }
                        });
    }

    /**
     * status:1为关注 2取消关注
     */
    public void requestFollow(){
        Integer status = 1;
        if (null == userEntity){
            return;
        }
        if (YStringUtil.eq(1, userEntity.getGuanzhuState())) {
            status = 2;
        } else if (YStringUtil.eq(2, userEntity.getGuanzhuState())) {
            status = 1;
        }
        Map<String, String> map = new HashMap<>();
        map.put("userid", userEntity.getId()+"");
        map.put("leixing", String.valueOf(status));

        Integer finalStatus = status;
        HttpUtils.getInstance()
                .data(model.follow(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                userEntity.setGuanzhuState(finalStatus);
                                uc.onFollowEvent.call();
                            }
                        });
    }

    public void clearChat(){
        V2TIMManager.getMessageManager().clearC2CHistoryMessage(txUserId, new V2TIMCallback() {
            @Override
            public void onSuccess() {
                YToastUtil.showShort(R.string.success_delete);
                YBusUtil.chatClearMessage();
            }


            @Override
            public void onError(int code, String desc) {
                YToastUtil.showShort("删除失败");
            }
        });
    }



}
